package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 结算单明细控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model){
        //获取结算的对象数据
        Statement statement = statementService.getById(statementId);
        //从用户数据中的支付id把值传递给老数据
        statement.setPayee(userService.get(statement.getPayeeId()));
        //把数据显示给页面
        model.addAttribute("statement",statement);
        //判断是否为消费，如果为消费则跳转到录入页面
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            return prefix + "editDetail";
        }else {
            //支付
            return prefix + "showDetail";
        }
    }

    @ResponseBody
    @RequestMapping("/query")
    public TablePageInfo<StatementItem> itemDetail(StatementItemQuery qo,Integer statementId){
        qo.setStatementId(statementId);
        return statementItemService.query(qo);
    }

    /**
     * 保存功能
     * @param items
     * @return
     */
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items){
         statementItemService.save(items);
         return AjaxResult.success();
    }

    /**
     * 支付功能
     * @param statementId
     * @return
     */
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId){
        statementItemService.payStatement(statementId);
        return AjaxResult.success();
    }


}
